package com.demo.webapp.kcfinder.config;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.digester3.Digester;
import org.apache.commons.digester3.binder.DigesterLoader;
import org.apache.commons.digester3.xmlrules.FromXmlRulesModule;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import com.demo.common.ClassLoaderUtils;

/**
 * KCFinder的配置管理器
 * 对配置信息进行管理
 * @author ryuu.kk
 *
 */
public class ConfigurationManager {

	protected static final Log LOG = LogFactory.getLog(ConfigurationManager.class);
	private static final String CONFIGURATION_FILE_NAME = "classpath:com/demo/webapp/kcfinder/config/demo-config.xml";
	private static final String CONFIGURATION_RULE_FILE = "classpath:com/demo/webapp/kcfinder/config/digester-rule.xml";
	
	protected Configuration configuration;
	private ClassLoader classLoader;
	private String configFile = CONFIGURATION_FILE_NAME;
	
	public ConfigurationManager() {
		// 构造函数
		this.classLoader = Thread.currentThread().getContextClassLoader();
	}
	
	public ConfigurationManager(String file) {
		// 构造函数
		this.configFile = file;
		this.classLoader = Thread.currentThread().getContextClassLoader();
	}
	
	public ConfigurationManager(String file, ClassLoader loader) {
		// 构造函数
		this.configFile = file;
		this.classLoader = loader;
	}
	
	public ClassLoader getClassLoader() {
		return classLoader;
	}

	public Configuration getConfiguration() {
		return configuration;
	}

	public void load() throws ConfigurationException {
		init();
	}

	public void destroy() {}
	
	protected void init() {

		//初始化配置对象
		//注意：如果需要扩展,这里需要让栈顶对象可以设置(目前使用DefaultConfiguration作为栈顶对象)
		configuration = new DefaultConfiguration();
		
		// DigesterLoader loader = DigesterLoader.newLoader(new
		// AbstractRulesModule() {
		// //@Override
		// //protected void loadRules() {
		// // //loadXMLRules(getInputStream(this.getClass(),
		// getRealPath(CONFIGURATION_RULE_FILE)));
		// //}
		// @Override
		// protected void configure() {
		// forPattern("actionConfig/action").createObject()
		// .ofType(com.demo.webapp.kcfinder.config.ActionConfig.class)
		// .usingConstructor("java.lang.String","java.lang.String","java.lang.String","java.lang.String")
		// .then().callParam().fromAttribute("package").ofIndex(0)
		// .then().callParam().fromAttribute("class").ofIndex(1)
		// .then().callParam().fromAttribute("name").ofIndex(2)
		// .then().callParam().fromAttribute("method").ofIndex(3);//.then().setNext("addConfig");
		// forPattern("actionConfig/action").callParam().fromAttribute("package").then().setProperty("packageName");
		// forPattern("actionConfig/action").setNext("addConfig");
		// }
		// });
		DigesterLoader loader = DigesterLoader.newLoader(new FromXmlRulesModule() {
					@Override
					protected void loadRules() {
						try {
							loadXMLRules(ClassLoaderUtils.getResourceAsStream(CONFIGURATION_RULE_FILE, this.getClass()));
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						}
					}
				});
		loader.setClassLoader(this.classLoader != null ? this.classLoader
				: Thread.currentThread().getContextClassLoader());
		Digester digester = loader.newDigester();
		InputStream input = null;

		try {
			// 读取资源
			input = ClassLoaderUtils.getResourceAsStream(configFile, this.getClass());

			// 设置栈顶对象
			digester.push(configuration);
			// FileSystemResourceLoader resourceLoader = new
			// FileSystemResourceLoader();
			// Resource resource = resourceLoader.getResource(getConfig());
			// digester.parse(resource.getInputStream());
			digester.parse(input);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
				}
			}
		}
	}

	public static void main(String[] args) {
		ConfigurationManager manager = new ConfigurationManager();
		manager.load();
	}
}
